#!/usr/bin/Rscript

# This script downloads the 24 QPF published by the CNRFC
# and saves it to the file rainfall.
# The script checks if the value in the file already exists 
# for a day so no duplicate values get saved 
 

tmpfile = "/home/acvmarsh/public_html/inflow/TEMP_FILE"
tmpfile = "./TEMP_FILE"
outfile = "/home/acvmarsh/public_html/inflow/rainfall"
outfile = "./rainfall"

options(warn=-1)

download.file("http://www.cnrfc.noaa.gov/awipsProducts/RNOWRKQPE_printer.php",tmpfile)

lines = readLines(tmpfile)

line = grep("MAD - ARCATA LOCAL",lines)

line = lines[line]
line = strsplit(line," ")[[1]]
line = line[line!='']
line = line[!is.na(as.numeric(line))]
line = as.numeric(line)

yesterdayObs = line[1]
todayForecast = line[2]

date = Sys.Date()

    #count the number of lines in the file and 
    #read in the last two lines in the file 
n = as.integer(system(paste('wc -l <',outfile),intern=T))
lastTwo = scan(outfile,what="character",sep="@",skip=(n-2),quiet=T)
lastTwoNums = scan(outfile,what="character",skip=(n-2),quiet=T)
lastTwoNums = as.numeric(lastTwoNums)
lastTwoNums = lastTwoNums[!is.na(lastTwoNums)]

today = c(as.character(date),todayForecast)
tp = sprintf("%s %4.2f",today[1],today[2])

a = grep(today[1],lastTwo)
if(length(a)==0){
    #then today's data has not been added
    b = grep(as.character(date-1),lastTwo)

    if(length(b)>0){
        #Remove the last line in the file (the old forecasted value) and print the two 
        #new values (yesterday's observed and today's forecast)
        yesterdayForecast <- if(length(lastTwoNums)==3) 
                            lastTwoNums[3]
                       else stop('Something is wrong with the output file.')

        yesterday = c(as.character(date-1),yesterdayForecast,yesterdayObs)
        yp = sprintf("%s %4.2f %4.2f",yesterday[1],yesterday[2],yesterday[3])

        system(paste('grep -v',yesterday[1],outfile,'>',tmpfile))
        system(paste('cat',tmpfile,'>',outfile))
        cat(yp,'\n',file=outfile,append=T)
        cat(tp,'\n',file=outfile,append=T)
        cat('Replaced yesterdays forecast and added todays.\n')
    }else{
        yesterdayForecast <- lastTwoNums[1]

        yesterday = c(as.character(date-1),yesterdayForecast,yesterdayObs)
        yp = sprintf("%s %4i %4.2f",yesterday[1],-9,yesterday[2])

        system(paste('grep -v',yesterday[1],outfile,'>',tmpfile))
        system(paste('cat',tmpfile,'>',outfile))
        cat(yp,'\n',file=outfile,append=T)
        cat(tp,'\n',file=outfile,append=T)
        cat('Yesterday data was not in the file, but todays was.\n')
        cat('There may be holes in the data.\n')
        cat('Added yesterdays observed and todays forecasted.\n')
    }

}else{
    #remove the last two lines in the file with the current values
    #though they are likely the same
    yesterdayForecast <- if(length(lastTwoNums)==3) 
                            lastTwoNums[1]
                       else stop('Something is wrong with the output file.')
    yesterday = c(as.character(date-1),yesterdayForecast,yesterdayObs)
    yp = sprintf("%s %4.2f %4.2f",yesterday[1],yesterday[2],yesterday[3])

    system(paste('grep -v',today[1],outfile,'>',tmpfile))
    system(paste('grep -v',yesterday[1],tmpfile,'>',outfile))
    cat(yp,'\n',file=outfile,append=T)
    cat(tp,'\n',file=outfile,append=T)
    cat('Todays value was already in the file but it was replaced anyway.\n')
    
}


dummy = file.remove(tmpfile)

